package com.swanclouds.redfish.managers;

import com.google.common.base.Preconditions;
import com.swanclouds.api.managers.KvmService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.core.transport.HttpResponse;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.managers.ExportKvmFileReq;
import com.swanclouds.model.managers.Kvm;
import com.swanclouds.model.managers.Oem.BMC.UpdateKvmService;
import com.swanclouds.model.managers.OpenH5KVM;
import com.swanclouds.model.managers.SetKvmKey;
import com.swanclouds.redfish.internal.BaseManagerService;
import com.swanclouds.redfish.managers.domain.KvmServiceModel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class KvmServiceImpl  extends BaseManagerService implements KvmService {

    /**
     * 查询kvm服务详情信息
     * @return Kvm
     */
    @Override
    public Kvm get() {
        return get(KvmServiceModel.class,uri(ClientConstants.PATH_MANAGERS_KVMSERVICE,getManagerId())).execute();
    }

    /**
     * 修改KVM服务信息
     * @param updateKvmService 修改KvmService所需参数
     * @return Kvm
     */
    @Override
    public Kvm updateKvm(UpdateKvmService updateKvmService) {
        Preconditions.checkNotNull(updateKvmService,"kvm is null");
        return patch(KvmServiceModel.class,uri(ClientConstants.PATH_MANAGERS_KVMSERVICE,getManagerId()))
                .entity(updateKvmService)
                .execute();
    }

    /**
     * 导出 KVM 启动文件
     *
     * @param export   条件
     * @param filename 下载文件
     * @return response
     */
    @Override
    public ActionResponse download(ExportKvmFileReq export, File filename) {
        Preconditions.checkNotNull(export, "KVM设置条件 is null");
        Preconditions.checkNotNull(filename);
        HttpResponse response = post(Void.class, uri(ClientConstants.MANAGERS_EXPORT_KVM_STARTUP_FILE, getManagerId()))
                .header(ClientConstants.HEADER_CONTENT_TYPE, ClientConstants.CONTENT_TYPE_JSON_CHARSET)
                .entity(export)
                .executeWithResponse();
        if (response.getStatus() < 400) {
            InputStream inputStream = response.getInputStream();
            OutputStream outputStream;
            try {
                outputStream = new FileOutputStream(filename);
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                inputStream.close();
                outputStream.flush();
                outputStream.close();
                return ActionResponse.actionSuccess();
            } catch (Exception e) {
                e.printStackTrace();
                return ActionResponse.actionFailed("Failed to write to file " + e.getMessage(), 400);
            }
        } else {
            return ActionResponse.actionFailed(response.getStatusMessage(),response.getStatus());
        }
    }

    /**
     * 打开H5kvm
     * @param openH5KVM [username和Web登录用户名一致最多一个符号  password 和Web登录密码一致]
     * @return Kvm
     */
    //username和Web登录用户名一致最多一个符号&  password和Web登录密码一致 上面注释&打出来打不了包所以放这与上面param参数注释意思相同
    @Override
    public Kvm openH5Kvm(OpenH5KVM openH5KVM) {
        Preconditions.checkNotNull(openH5KVM.getUserName(), "userName is null");
        Preconditions.checkNotNull(openH5KVM.getPassWord(), "password is null");
        return post(KvmServiceModel.class,uri(ClientConstants.PATH_MANAGERS_OPENH5KVM,getManagerId())).entity(openH5KVM).execute();
    }

    /**
     * 设置KVM key
     * @param setKvmKey 设置参数 id,idExit,SecretKey,Mode
     * @return Kvm
     */
    @Override
    public Kvm setKvmKey(SetKvmKey setKvmKey) {
        Preconditions.checkNotNull(setKvmKey.getId(), "id is null");
        Preconditions.checkNotNull(setKvmKey.getIdExt(), "idExt is null");
        Preconditions.checkNotNull(setKvmKey.getSecretKey(), "secretKey is null");
        Preconditions.checkNotNull(setKvmKey.getMode(), "mode is null");
        return post(KvmServiceModel.class,uri(ClientConstants.PATH_MANAGERS_SETKVMKEY,getManagerId())).entity(setKvmKey).execute();
    }
}
